<?php
/**
 * Padd Twitter Class
 * 
 * The code is based on the the Wordpress plugin Twitter for Wordpress by Ricardo Gonzalez.
 */
class Padd_Twitter {

	public static function get_messages($username='',$num=1,$list=false) {

		include_once ABSPATH . WPINC . '/rss.php';
                $messages->items = true;
		//$messages = fetch_rss('http://twitter.com/statuses/user_timeline/'.$username.'.rss');
		if ($list) {
			echo '<ul class="padd-twitter">';
		}
		//var_dump($messages);exit();
		if ($username == '') {
			if ($list) { 
				echo '<li>'; 
			}
			echo 'Twitter settings is not configured.';
			if ($list) { 
				echo '</li>';
			}
		} else {
			if ( empty($messages->items) ) {
				if ($list) { 
					echo '<li>'; 
				}
				echo 'No public Twitter messages.';
				if ($list) { 
					echo '</li>';
				}
			} else {
                            $pos = substr_count($username, '@');
                            if($pos == 0){
                                $username = '@'.$username;
                            }
                            echo "
                                <!--Botao follow do twitter-->
                                        <a href='https://twitter.com/".$username."' class='twitter-follow-button' data-show-count='false' data-lang='pt' data-size='large'>Seguir ".$username."</a><br/><br/>
<script>
new TWTR.Widget({
  version: 2,
  type: 'profile',
  rpp: 4,
  interval: 30000,
  width: 230,
  height: 215,
  theme: {
    shell: {
      background: '#333333',
      color: '#ffffff'
    },
    tweets: {
      background: '#000000',
      color: '#ffffff',
      links: '#4aed05'
    }
  },
  features: {
    scrollbar: true,
    loop: false,
    live: true,
    behavior: 'default'
  }
}).render().setUser('".$username."').start();
</script>";
			}
			if ($list) {
				echo '</ul>';
			}
		}
	}

	public static function parse_hyperlinks($text) {
		// Props to Allen Shaw & webmancers.com
		// match protocol://address/path/file.extension?some=variable&another=asf%
		//$text = preg_replace("/\b([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)\b/i","<a href=\"$1\" class=\"twitter-link\">$1</a>", $text);
		$text = preg_replace('/\b([a-zA-Z]+:\/\/[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i',"<a href=\"$1\" class=\"twitter-link\">$1</a>", $text);
		// match www.something.domain/path/file.extension?some=variable&another=asf%
		//$text = preg_replace("/\b(www\.[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)\b/i","<a href=\"http://$1\" class=\"twitter-link\">$1</a>", $text);
		$text = preg_replace('/\b(?<!:\/\/)(www\.[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i',"<a href=\"http://$1\" class=\"twitter-link\">$1</a>", $text);    
		
		// match name@address
		$text = preg_replace("/\b([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]*\@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})\b/i","<a href=\"mailto://$1\" class=\"twitter-link\">$1</a>", $text);
		//mach #trendingtopics. Props to Michael Voigt
		$text = preg_replace('/([\.|\,|\:|\�|\�|\>|\{|\(]?)#{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"http://twitter.com/#search?q=$2\" class=\"twitter-link\">#$2</a>$3 ", $text);
		return $text;
	}

	public static function parse_twitter_users($text) {
		$text = preg_replace('/([\.|\,|\:|\�|\�|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"http://twitter.com/$2\" class=\"twitter-user\">@$2</a>$3 ", $text);
		return $text;
	} 

}

?>